﻿using Inovout.Runtime;
using System;

namespace Inovout.Security
{
    public class ClaimedIdentifier
    {
        public string Code { get; set; }

        public override bool Equals(object obj)
        {
            ClaimedIdentifier that = (ClaimedIdentifier)obj;
            return this.Code.Equals(that.Code);
        }
        public override int GetHashCode()
        {
            return Code.GetHashCode();
        }
        public static implicit operator ClaimedIdentifier(Uri identifier)
        {
            return Parse(identifier);
        }
        public static implicit operator ClaimedIdentifier(string identifier)
        {
            return Parse(identifier);
        }
        public static ClaimedIdentifier Parse(string uri)
        {
            Uri claimedIdentifierUri;
            if (Uri.TryCreate(uri, UriKind.Absolute, out claimedIdentifierUri))
            {
                return Parse(new Uri(uri));
            }
            throw new ArgumentException("uri");
        }

        public static ClaimedIdentifier Parse(Uri uri)
        {
            string[] paths = uri.LocalPath.Trim('/').Split('/');
            var result = new ClaimedIdentifier();
            result.Code = paths[paths.Length - 1];
            return result;
        }

        //private static readonly string systemUserIdentityFormat;
        //static ClaimedIdentifier()
        //{
        //    systemUserIdentityFormat = ComponentRegistry.Resolve<IApplication>().Settings["SystemUserIdentityFormat"].ToString();
        //}
        //public override string ToString()
        //{
        //    return string.Format(systemUserIdentityFormat, IdentifierProviederName, UserNameOfProvider);
        //}
    }
}
